﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NENAData;
using NENAData.NENAMember;
using NENAData.Events;
using NENAData.Factory;
using NENAData.Transactions.Payments.PayPal;

namespace Events.Register
{
    public partial class RegistrationForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Member_ID"] == null)
            {
                Response.Redirect("../default.aspx", true);
            }

            if (!IsPostBack)
                SetupDefaults();
        }

        private void SetupDefaults()
        {
            NENAMember oNenaData = new NENAMember();
            var oMember = oNenaData.GetMember((int)Session["Member_ID"]);

            PersonalFirstName.Text = oMember.FirstName;
            PersonalLastName.Text = oMember.LastName;
            PersonalHomePhone.Text = oMember.HomePhone;
            PersonalCity.Text = oMember.City;
            PersonalPostalCode.Text = oMember.PostalCode;
            PersonalProvince.Text = oMember.Province;
            PersonalEmail.Text = oMember.EmailAddress;
            PersonalAddress1.Text = oMember.Address;
            PersonalAddress2.Text = oMember.Address2;

            ListEvents();
            LoadSessions();
            SetConferenceFee();
            CalculateRegistrationFee(null, null);
        }

        private void SetConferenceFee()
        {
            var o = new NENAData.Events.List();
            bool boolMember = MemberStatusToBool();
            var oEvents = o.GetAll(boolMember, NENAData.Transactions.TransactionList.Transactions.ConferenceRegina2012,1 );

            foreach (var item in oEvents)
            {
                Session["EventFee"] = item.Fee.ToString();
                Session["EventId"] = item.EventId.ToString();
                Session["PaymentTo"] = item.Affiliate.ToString();
            }
        }

        private bool MemberStatusToBool()
        {
            if (Session["PaidMember"] == "False")
                return false;
            else
                return true;
        }
        protected void SaveRegistrationForm(object sender, EventArgs e)
        {
            PayPalTransactions oPPTransaction = new PayPalTransactions();
            //NENA_TransactionID hardcoded to 7 - 2011 conference
            int intTransactionPayment = oPPTransaction.NewTemplatePayPal(Session["TotalEventFee"].ToString(), Convert.ToInt32(Session["Member_ID"]), Convert.ToInt32(Session["PaymentTo"]), 7);
            Session["TransactionPaymentID"] = intTransactionPayment;
            //oEventRegistration.AddbySQL((int)Session["Member_ID"], (int)Session["EventId"], intTransactionPayment, GenerateRegistrationComments());

            EventRegistration oEventRegistration = new EventRegistration();
            string strGenerateComments = GenerateRegistrationComments();
            int intEventRegistrationId = oEventRegistration.AddbyStoredProcedure(Convert.ToInt32(Session["Member_ID"]), Convert.ToInt32(Session["EventId"]), intTransactionPayment, strGenerateComments);

            //save event social tickets
            if (Session["EventSocial"] == "True")
            {
                int intTickets = Convert.ToInt32(DropDownListTickets.SelectedValue);
                for (int i = 1; i <= intTickets; i++)
                {
                    oEventRegistration.AddbyStoredProcedure(Convert.ToInt32(Session["Member_ID"]), Convert.ToInt32(CheckBoxListEventSelection.SelectedValue), intTransactionPayment, "");
                }

            }

            SaveSession(Convert.ToInt32(RadioButtonListSession1.SelectedValue), intEventRegistrationId);
            SaveSession(Convert.ToInt32(RadioButtonListSession2.SelectedValue), intEventRegistrationId);
            SaveSession(Convert.ToInt32(RadioButtonListSession3.SelectedValue), intEventRegistrationId);
            SaveSession(Convert.ToInt32(RadioButtonListSession4.SelectedValue), intEventRegistrationId);
            SaveSession(Convert.ToInt32(RadioButtonListSession5.SelectedValue), intEventRegistrationId);

            //redirect to PayPal to complete payment
            RedirectPayPal();
        }

        private void RedirectPayPal()
        {
            string strRedirect = "http://membership.nena.ca/paypal/expresscheckout.aspx";
            //string strRedirect = "http://localhost:3208/expresscheckout.aspx";

            strRedirect += "?Member_ID=" + Session["Member_ID"].ToString();
            strRedirect += "&Affiliate_ID=" + Session["PaymentTo"].ToString();
            //strRedirect += "&payment_amt=1";
            strRedirect += "&payment_amt=" + String.Format("{0:00.00}", Session["TotalEventFee"]);
            //transactionid_nena 7 - NENA 2011 conference
            strRedirect += "&TransactionID_NENA=7";
            strRedirect += "&EventID=" + Session["EventId"];
            strRedirect += "&TransactionPaymentID=" + Session["TransactionPaymentID"];

            Response.Redirect(strRedirect, false);

        }
        private void SaveSession(Int32 intSessionId, int intEventRegistrationId)
        {
            SessionRegistration oSessionRegistration = new SessionRegistration();
            NENAData.EF.EventSessionRegistration oEventSessionRegistration1 = new NENAData.EF.EventSessionRegistration();

            oSessionRegistration.AddbySQL(intSessionId, intEventRegistrationId, "");

        }

        private static NENAData.EF.NENA_MembershipEntities GetConnection()
        {
            var x = new SQLMembership();
            return x.DBConnection();
        }
        private string GenerateRegistrationComments()
        {
            string strNCACBreakfast;
            if (CheckBoxNCACBreakfast.Checked)
                strNCACBreakfast = "will";
            else
                strNCACBreakfast = "will not";

            string strComment = "My April 30th entree selection is " + TextboxDietary.Text.ToString();
            strComment += ";" + Environment.NewLine + "I " + strNCACBreakfast + " be attending the NCAC breakfast Saturday Morning.";
            return strComment;
        }

        protected void CalculateRegistrationFee(object sender, EventArgs e)
        {

            var o = new NENAData.Events.List();
            decimal decimalFee = 0;

            foreach (ListItem EventItem in CheckBoxListEventSelection.Items)
            {
                if (EventItem.Selected)
                {
                    if (DropDownListTickets.SelectedValue == "0")
                        DropDownListTickets.SelectedIndex = 1;

                    Session["EventSocial"] = "True";
                    var NENAEvent = o.GetEvent(Convert.ToInt32(EventItem.Value));
                    decimalFee += NENAEvent.Fee * Convert.ToInt32(DropDownListTickets.SelectedValue);
                }
                else
                    Session["EventSocial"] = "False";
            }
            decimalFee += Convert.ToDecimal(Session["EventFee"]);
            Session["TotalEventFee"] = decimalFee;
            //Response.Write(decimalFee);
            txtCost.Text = decimalFee.ToString();
        }

        protected void ListEvents()
        {
            var o = new NENAData.Events.List();
            //RadGrid1.MasterTableView = o.GetAll(true, NENAData.Transactions.TransactionList.Transactions.ConferenceRegina2011);
            //RadGrid1.DataSource = o.GetAll(true, NENAData.Transactions.TransactionList.Transactions.ConferenceRegina2011);
            bool boolMember = MemberStatusToBool();

            //bool boolMember = true;
            //Response.Write(Session["PaidMember"]);
            GridView1.DataSource = o.GetAll(boolMember, NENAData.Transactions.TransactionList.Transactions.ConferenceRegina2012);
            GridView1.DataBind();

            CheckBoxListEventSelection.DataSource = o.GetAll(boolMember, NENAData.Transactions.TransactionList.Transactions.ConferenceRegina2012, 2);
            CheckBoxListEventSelection.DataTextField = "Description";
            CheckBoxListEventSelection.DataValueField = "EventId";
            CheckBoxListEventSelection.DataBind();
        }

        private void LoadSessions()
        {
            //group 1
            var oSlotSession = new NENAData.Events.SlotSession();
            GridViewSession1.DataSource = oSlotSession.GetSlotId(1, true);
            GridViewSession1.DataBind();
            
            RadioButtonListSession1.DataSource = oSlotSession.GetSlotId(1, true);
            RadioButtonListSession1.DataTextField = "Description";
            RadioButtonListSession1.DataValueField = "SessionId";
            RadioButtonListSession1.DataBind();
            //group 2
            GridViewSession2.DataSource = oSlotSession.GetSlotId(2, true);
            GridViewSession2.DataBind();

            RadioButtonListSession2.DataSource = oSlotSession.GetSlotId(2, true);
            RadioButtonListSession2.DataTextField = "Description";
            RadioButtonListSession2.DataValueField = "SessionId";
            RadioButtonListSession2.DataBind();
            //group 3
            GridViewSession3.DataSource = oSlotSession.GetSlotId(3, true);
            GridViewSession3.DataBind();

            RadioButtonListSession3.DataSource = oSlotSession.GetSlotId(3, true);
            RadioButtonListSession3.DataTextField = "Description";
            RadioButtonListSession3.DataValueField = "SessionId";
            RadioButtonListSession3.DataBind();
            //group 4
            GridViewSession4.DataSource = oSlotSession.GetSlotId(4, true);
            GridViewSession4.DataBind();

            RadioButtonListSession4.DataSource = oSlotSession.GetSlotId(4, true);
            RadioButtonListSession4.DataTextField = "Description";
            RadioButtonListSession4.DataValueField = "SessionId";
            RadioButtonListSession4.DataBind();
            //group 5
            GridViewSession5.DataSource = oSlotSession.GetSlotId(5, true);
            GridViewSession5.DataBind();

            RadioButtonListSession5.DataSource = oSlotSession.GetSlotId(5, true);
            RadioButtonListSession5.DataTextField = "Description";
            RadioButtonListSession5.DataValueField = "SessionId";
            RadioButtonListSession5.DataBind();

        }
        private void PopulateChecklist()
        {
            //CheckBoxListEventSelection
        }
    }
}